package com.runtime.hongyunmanage.controller;

import com.runtime.hongyunmanage.bean.Users;
import com.runtime.hongyunmanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping
    public Result findUser(Integer currentPage, Integer pageSize,@RequestBody Users users){

        int offset = (currentPage - 1) * pageSize;
        List<Users> list=userService.findUsersByCondition(pageSize,offset,users);
        int totals = userService.CountUsers(users);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("totals", totals);
        Integer code= null != list && list.size()> 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = null != list && list.size() > 0 ? "查询用户列表成功" : "查询用户列表失败";
        return new Result(code,map,msg);
    }


    @PutMapping
    public Result reset(@RequestBody Users users){
        users.setPasswordHash("12345678");
        // 1. 生成盐值
        String salt = PasswordUtil.generateSalt();

        // 2. 使用盐值哈希密码
        String hashedPassword = PasswordUtil.hashWithSalt(users.getPasswordHash(), salt);

        // 3. 组合盐值和哈希值以便存储
        String storedPassword = PasswordUtil.combineSaltAndHash(salt, hashedPassword);

        System.out.println("存储的密码格式: " + storedPassword);
        users.setPasswordHash(storedPassword);
        boolean flag = userService.updateById(users);
        Integer code=flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? "重置密码成功" : "重置密码失败";
        return new Result(code,flag,msg);
    }

    @DeleteMapping("/{id}")
    public Result deleteSingle(@PathVariable Integer id){
        boolean flag = userService.removeById(id);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = flag ? "删除管理员资料成功" : "删除管理员资料失败";
        return new Result(code, flag, msg);
    }

    @PutMapping("/status")
    public Result updateStatus(@RequestBody Users users){
        boolean flag = userService.updateById(users);
        Integer code=flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? "修改客户资料成功" : "修改客户资料失败";
        return new Result(code,flag,msg);
    }

    @PutMapping("/add")
    public Result add(@RequestBody Users users){


        // 1. 检查用户名是否已存在
        boolean exists = userService.existsByUsername(users.getUsername());
        if (exists) {
            return new Result(Code.SAVE_ERR, null, "用户名已存在");
        }

        users.setPasswordHash("12345678");
        // 1. 生成盐值
        String salt = PasswordUtil.generateSalt();

        // 2. 使用盐值哈希密码
        String hashedPassword = PasswordUtil.hashWithSalt(users.getPasswordHash(), salt);

        // 3. 组合盐值和哈希值以便存储
        String storedPassword = PasswordUtil.combineSaltAndHash(salt, hashedPassword);
        users.setPasswordHash(storedPassword);
        boolean flag = userService.save(users);
        Integer code=flag ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag ? "增加管理员资料成功" : "增加管理员资料失败";
        return new Result(code,flag,msg);
    }


    @PutMapping("/update")
    public Result update(@RequestBody Users users){
        boolean exists = userService.existsByUsername(users.getUsername());
        if (exists) {
            return new Result(Code.SAVE_ERR, null, "用户名已存在");
        }
        boolean flag = userService.updateById(users);
        Integer code=flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? "修改客户资料成功" : "修改客户资料失败";
        return new Result(code,flag,msg);
    }

}
